-
Notifications
You must be signed in to change notification settings - Fork 23.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Jinja2 tests for testing types #37517
Conversation
32c5efd
to
df36426
Compare
df36426
to
66ccd11
Compare
459373b
to
9fc9cd1
Compare
27375cc
to
571fa20
Compare
lib/ansible/plugins/test/core.py
Outdated
|
||
|
||
# NOTE: The existing Jinja2 'sequence' test matches strings and dictionaries | ||
def test_list(value): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that #39924 has been merged into devel you can:
from functools import partial
from ansible.module_utils.common.collections import is_sequence
test_list = partial(is_sequence, include_strings=False)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not impressed, sorry. I don't think this will make it easier to upstream. I don't need an Ansible-only solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not talking about upstream. This is about this PR, which currently doesn't need to have duplicate code, like one in devel.
) * Also marking non-string defaults. * Adding list filter from #37517 to plugin_formatter. * Simplifying list test. * Redistribute imports
…ible#40212) * Also marking non-string defaults. * Adding list filter from ansible#37517 to plugin_formatter. * Simplifying list test. * Redistribute imports (cherry picked from commit 0752dc1)
#42075) * Documentation: show non-string non-iterable defaults for choices (#40212) * Also marking non-string defaults. * Adding list filter from #37517 to plugin_formatter. * Simplifying list test. * Redistribute imports (cherry picked from commit 0752dc1) * for 2.6 compatibility, removes dependency on collections.py, take two * fix blank line error
dfbb592
to
a8ac047
Compare
This comment has been minimized.
This comment has been minimized.
a8ac047
to
834dc91
Compare
@@ -0,0 +1 @@ | |||
shippable/posix/ci/group1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shippable/posix/group1
So Jinja2 only has tests for testing string types, or iterable types, which is quite limiting. If you need to test a boolean value in Jinja2 the only option is to test it is not a string and convert it to a string and compare it to 'True' or 'False'. So these tests are essential constructs in playbooks and templates. This PR includes integration tests to validate Jinja2 behaviour.
834dc91
to
43e716b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not tolerate copy-paste driven development
|
||
|
||
# NOTE: The existing Jinja2 'sequence' test also matches strings and dictionaries | ||
def test_list(value): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is already available in ansible.module_utils.common.collections.is_sequence
for broader use.
So please follow earlier suggestion to reuse it: #37517 (comment)
So instead of duplicating tests to allow older versions of Jinja2 to work the same, but mask changes/implementation details on newer ones, we've opted to add a 'jinja2' collection with these kind of tests/filters that are missing from older versions. temporarily at https://galaxy.ansible.com/sivel/jinja2 |
SUMMARY
So Jinja2 only has tests for testing string types, or iterable types,
which is quite limiting. If you need to test a boolean value in Jinja2
the
onlyoption is to test it is not a string and convert it to a stringand compare it to 'True' or 'False'.
So these tests are essential constructs in playbooks and templates.
This includes tests for:
So you can simply do things like:
or
Just like you already can do
and
See the included integration tests for a complete explanation of what Jinja2 already can do, and how this is insufficient in some cases where this would be useful.
This is related to #37514
ISSUE TYPE
COMPONENT NAME
Jinja2 core tests
ANSIBLE VERSION
v2.6